package com.devunion.bogdan.view.project.actions;

import com.devunion.bogdan.view.actions.ProjectSaveAction;
import com.devunion.bogdan.view.actions.LoginDlgAction;
import com.devunion.bogdan.view.actions.LoginAction;

import javax.swing.*;
import java.util.Map;
import java.util.HashMap;

/**
 * @author Viktar Liaskovich
 */
public class ProjectActionFactory {
    public static final String RECENT_PROJECTS_ACTION = "RECENT_PROJECTS_ACTION";
    public static final String NEW_PROJECT_ACTION = "NEW_PROJECT_ACTION";
    public static final String DELETE_PROJECT_ACTION = "DELETE_PROJECT_ACTION";
    public static final String CLOSE_PROJECT_ACTION = "CLOSE_PROJECT_ACTION";
    public static final String VIEW_PROJECTS_BASE_ACTION = "VIEW_PROJECTS_BASE_ACTION";
    public static final String MANAGE_PROJECTS_ACTION = "MANAGE_PROJECTS_ACTION";
    public static final String SAVE_PROJECT_ACTION = "SAVE_PROJECT_ACTION";
    public static final String LOGIN_DLG_ACTION = "LOGIN_DLG_ACTION";
    public static final String LOGIN_ACTION = "LOGIN_ACTION";

    private static Map<String, AbstractAction> actionCache = new HashMap<String, AbstractAction>();

    public static AbstractAction createAction(String actionName) {
        AbstractAction action = actionCache.get(actionName);

        if (action == null) {
            action = createActionByName(actionName);
            actionCache.put(actionName, action);
        }

        return action;

    }

    private static AbstractAction createActionByName(String actionName) {
        if (NEW_PROJECT_ACTION.equals(actionName)) {
            return new NewProjectAction();
        }
        if (DELETE_PROJECT_ACTION.equals(actionName)) {
            return new DeleteProjectAction();
        }

        if (RECENT_PROJECTS_ACTION.equals(actionName)) {
            return new RecentProjectsAction();
        }

        if (CLOSE_PROJECT_ACTION.equals(actionName)) {
            return new ProjectCloseAction();
        }

        if (VIEW_PROJECTS_BASE_ACTION.equals(actionName)) {
            return new ViewProjectsBaseAction();
        }

        if (MANAGE_PROJECTS_ACTION.equals(actionName)) {
            return new ManageProjectsAction();
        }

        if (SAVE_PROJECT_ACTION.equals(actionName)) {
            return new ProjectSaveAction();
        }

        if (LOGIN_DLG_ACTION.equals(actionName)) {
            return new LoginDlgAction();
        }

        if (LOGIN_ACTION.equals(actionName)) {
            return new LoginAction();
        }

        throw new IllegalArgumentException("Unknown action with name: '" + actionName + "'");
    }
}
